package com.ds.topmenu.database;

import com.ds.admin.db.nav.DBNavTreeView;
import com.ds.common.JDSException;
import com.ds.common.util.StringUtility;
import com.ds.config.DSMResultModel;
import com.ds.config.ResultModel;
import com.ds.config.TreeListResultModel;
import com.ds.esd.client.ESDClient;
import com.ds.esd.client.ESDFacrory;
import com.ds.esd.custom.annotation.CustomAnnotation;
import com.ds.esd.custom.annotation.DialogAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.api.enums.RequestPathEnum;
import com.ds.esd.custom.module.annotation.DynLoadAnnotation;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.custom.nav.annotation.NavTreeViewAnnotation;
import com.ds.esd.custom.toolbar.MenuBarMenu;
import com.ds.esd.editor.enums.CustomMenuType;
import com.ds.esd.tool.ui.component.list.TreeListItem;
import com.ds.web.annotation.Aggregation;
import com.ds.web.annotation.AggregationType;
import com.ds.web.annotation.Split;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Controller
@RequestMapping(value = {"/action/database/"})
@MenuBarMenu(menuType = CustomMenuType.menubar, caption = "数据库", index = 2)
@Aggregation(type = AggregationType.menu)
public class DataBaseAction {

    @RequestMapping(value = {"ProviderList"}, method = {RequestMethod.POST})
    @ModuleAnnotation(caption = "数据源", imageClass = "iconfont iconchucun")
    @CustomAnnotation(index = 0)
    @DynLoadAnnotation(refClassName = "RAD.db.DBProviderList", projectName = "DSMdsm")
    @APIEventAnnotation(autoRun = true)
    public @ResponseBody
    ResultModel<Boolean> getProviderList() {
        ResultModel resultModel = new ResultModel();
        return resultModel;
    }

    @RequestMapping(value = {"split"})
    @Split
    @CustomAnnotation(index = 1)
    @ResponseBody
    public ResultModel<Boolean> split() {
        ResultModel resultModel = new ResultModel();
        return resultModel;
    }

    @RequestMapping(method = RequestMethod.POST, value = "DbManager")
    @APIEventAnnotation(autoRun = true)
    @NavTreeViewAnnotation
    @CustomAnnotation(index = 2)
    @ModuleAnnotation(imageClass = "spafont spa-icon-c-grid", caption = "库表管理", dynLoad = true)
    @ResponseBody
    public TreeListResultModel<List<DBNavTreeView>> getTabelManager(String id) {
        TreeListResultModel<List<DBNavTreeView>> resultModel = new TreeListResultModel<List<DBNavTreeView>>();
        DBNavTreeView<TreeListItem> dbItem = new DBNavTreeView();
        List<DBNavTreeView> items = new ArrayList<>();
        items.add(dbItem);
        if (id != null && !id.equals("")) {
            String[] orgIdArr = StringUtility.split(id, ";");
            resultModel.setIds(Arrays.asList(orgIdArr));
        } else if (dbItem.getSub() != null && dbItem.getSub().size() > 0) {
            resultModel.setIds(Arrays.asList(new String[]{dbItem.getSub().get(0).getId()}));
        }
        resultModel.setData(items);
        return resultModel;

    }

    @RequestMapping(value = {"importTable"}, method = {RequestMethod.POST})
    @ModuleAnnotation(caption = "导入库表", width = "800", height = "550", imageClass = "fa fa-database")
    @DynLoadAnnotation(refClassName = "RAD.db.project.DataBaseConfigList", projectName = "DSMdsm")
    @APIEventAnnotation(customRequestData = {RequestPathEnum.SPA_projectName})
    @CustomAnnotation(index = 3)
    @ResponseBody
    public DSMResultModel<Boolean> importTable(String projectName) {
        DSMResultModel resultModel = new DSMResultModel();
        return resultModel;
    }


    @RequestMapping(value = {"split2"})
    @Split
    @CustomAnnotation(index = 4)
    @ResponseBody
    public ResultModel<Boolean> split2() {
        ResultModel resultModel = new ResultModel();
        return resultModel;
    }


    @RequestMapping(value = {"SqlConsole"}, method = {RequestMethod.POST})
    @ModuleAnnotation(caption = "运行监控", width = "800", height = "550", imageClass = "spafont spa-icon-c-video")
    @DynLoadAnnotation(refClassName = "RAD.db.console.SqlConsole", projectName = "DSMdsm")
    @APIEventAnnotation(customRequestData = {RequestPathEnum.SPA_projectName})
    @CustomAnnotation(index = 5)
    @ResponseBody
    @DialogAnnotation
    public DSMResultModel<Boolean> sqlConsole(String projectName) {
        DSMResultModel resultModel = new DSMResultModel();
        return resultModel;
    }

    @RequestMapping(value = {"split3"})
    @Split
    @CustomAnnotation(index = 6)
    @ResponseBody
    public ResultModel<Boolean> split3() {
        ResultModel resultModel = new ResultModel();
        return resultModel;
    }


    @RequestMapping(method = RequestMethod.POST, value = "codeGen")
    @CustomAnnotation(index = 7)
    @MenuBarMenu(menuType = CustomMenuType.sub, caption = "代码生成", imageClass = "spafont spa-icon-formatjs")
    public CodeGenAction getCodeGenAction() {
        return new CodeGenAction();
    }


    public ESDClient getClient() throws JDSException {

        ESDClient client = ESDFacrory.getESDClient();

        return client;
    }


}
